#!/usr/bin/env bash

set -o pipefail

handle_interrupt() {
  echo "Caught ctrl-c, exiting"
  exit 1
}

trap 'handle_interrupt' SIGINT

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

function HELP {
    echo "help:"
    echo "-a <arch> : build for <arch> (default: all)"
    echo "-e        : build with WERROR=1"
    echo "-r        : also build DEBUG=0"
    echo "-q        : hide output of build"
    echo "-h        : for help"
    exit 1
}

RELEASE=0
WERROR=0
QUIET=0
ARCH="all"

while getopts a:ehrq FLAG; do
    case $FLAG in
        a) ARCH="$OPTARG";;
        e) WERROR=1;;
        h) HELP;;
        r) RELEASE=1;;
        q) QUIET=1;;
        \?)
            echo unrecognized option
            HELP
    esac
done

shift $((OPTIND-1))

echo > buildall.log
function log()
{
    if (( QUIET )); then
        "$@" >> buildall.log 2>&1
    else
        "$@" 2>&1 | tee -a buildall.log
    fi
}

# find all the projects in the project directory
_PROJECTS=$(echo project/*.mk | xargs -n1 basename | sed 's/\.mk//')
FAILED=""

# If ARCH is set to all, we build for all architectures, otherwise we
# filter projects based on the ARCH variable.
if [ "$ARCH" != "all" ]; then
    for p in $_PROJECTS; do
        # Look for ARCH = <arch> in the output of make list-arch
        PROJECT_ARCH="$(PROJECT=$p make list-arch | grep 'ARCH' | tail -1 | cut -d ' ' -f 3)"
        if [ "$PROJECT_ARCH" == "$ARCH" ]; then
            PROJECTS+="$p "
        else
            if (( !QUIET )); then
                echo "Skipping $p, not compatible with architecture $ARCH"
            fi
        fi
    done
else
    PROJECTS+="$_PROJECTS"
fi

echo projects to build: "$PROJECTS"

if (( WERROR )); then
    WERROR_MSG="with WERROR"
fi

for p in $PROJECTS; do
    echo "building $p $WERROR_MSG"
    WERROR=$WERROR PROJECT=$p log nice "$DIR"/make-parallel || FAILED="$FAILED $p"
done

if (( RELEASE )); then
for p in $PROJECTS; do
    echo "building $p-release $WERROR_MSG"
    BUILDDIR_SUFFIX=-release WERROR=$WERROR DEBUG=0 PROJECT=$p log nice "$DIR"/make-parallel || FAILED="$FAILED $p-release"
done
fi

# Print out at the end which projects failed to build
if [ "$FAILED" != "" ]; then
    echo
    echo some projects have failed to build:
    echo "$FAILED"
    exit 1
fi
